/**
 * 
 */
package ac.svn.accesscontrol.ui.window;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import ac.svn.accesscontrol.utils.UIUtility;
import ac.svn.accesscontrol.utils.Utility;

/**
 * @author Ankush Chaudhary
 *
 */
public class AccessRightsAssignmentWindow extends JDialog 
{

    /**
     * 
     */
    private JDialog Iam = this;
    private static final long serialVersionUID 	= 1L;
    private JLabel informationLabel 		= null;
    private JList  availableItemsView		= null;
    private JList  assignedItemsView		= null;
    private JButton okButton = null, addButton = null, removeButton = null;
    private JCheckBox readCheckBox, writeCheckBox;

    public AccessRightsAssignmentWindow() {
	super();
	setTitle("Access Rights");
	setModal(true);
	setSize(800,600);
	Utility.setLocationToMiddleOfScreen(this);
	initComponents();
    }

    private void initComponents()
    {
	Color WHITE = Color.WHITE;
	Border border = UIUtility.setAllShadowedBorder(null);
	
	getContentPane().setBackground(WHITE );
	
	String locAccessRightsTitle = "<HTML><p align=\"CENTER\">Welcome to location access rights editor, this dialog will help you in assigning and removing access rights for a location!</p></HTML>";
	informationLabel = new JLabel(locAccessRightsTitle);

	//Initialize lists.
	availableItemsView = new JList();
	availableItemsView.setBorder(border);
	assignedItemsView  = new JList();
	assignedItemsView.setBorder(border);
	
	//Initialize buttons.
	okButton  	= new JButton("Done");
	addButton 	= new JButton("+");
	removeButton 	= new JButton("-");

	readCheckBox  = new JCheckBox("R",false);
	readCheckBox.setBackground(WHITE);
	writeCheckBox = new JCheckBox("W",false);
	writeCheckBox.setBackground(WHITE);

	removeFocus(okButton);
	removeFocus(addButton);
	removeFocus(removeButton);
	removeFocus(readCheckBox);
	removeFocus(writeCheckBox);

	//Layout all elements.
	//Base for all elements
	JPanel basePanel = new JPanel((new BorderLayout(5,5)),true);
	basePanel.setBackground(WHITE);
	basePanel.setBorder(border);

	JPanel bodyBasePanel 	= new JPanel((new GridLayout(1,2,5,5)),true);
	bodyBasePanel.setBackground(WHITE);
	//bodyBasePanel.setBorder(border);

	JPanel headerPanel 	= new JPanel(new FlowLayout(FlowLayout.RIGHT),true);
	headerPanel.setBackground(WHITE);
	headerPanel.setBorder(border);
	
	JScrollPane scrollPane 	= new JScrollPane(availableItemsView);
	scrollPane.setBackground(WHITE);
	scrollPane.setBorder(border);
	
	headerPanel.add(new JLabel("Add"));
	headerPanel.add(addButton);
	
	scrollPane.setBorder(new TitledBorder(border,"Available Items"));
	scrollPane.setColumnHeaderView(headerPanel);

	bodyBasePanel.add(scrollPane);

	scrollPane 	= new JScrollPane(assignedItemsView);
	scrollPane.setBackground(WHITE);
	scrollPane.setBorder(border);
	
	headerPanel 	= new JPanel(new FlowLayout(FlowLayout.RIGHT),true);
	headerPanel.setBackground(WHITE);
	headerPanel.setBorder(border);
	
	headerPanel.add(new JLabel("Remove"));
	headerPanel.add(removeButton);
	headerPanel.add(readCheckBox);
	headerPanel.add(writeCheckBox);
	
	scrollPane.setBorder(new TitledBorder(border,"Assigned Items"));
	scrollPane.setColumnHeaderView(headerPanel);

	bodyBasePanel.add(scrollPane);

	basePanel.add(bodyBasePanel,BorderLayout.CENTER);

	headerPanel = new JPanel((new BorderLayout(5,5)),true);
	headerPanel.setBackground(WHITE);
	headerPanel.add(informationLabel,BorderLayout.CENTER);
	
	informationLabel.setBorder(new TitledBorder(border,"Operation Detail"));

	basePanel.add(headerPanel,BorderLayout.NORTH);

	JPanel buttonPanel = new JPanel();
	buttonPanel.setBackground(WHITE);
	buttonPanel.setBorder(border);
	buttonPanel.add(okButton);

	basePanel.add(buttonPanel,BorderLayout.SOUTH);

	setLayout(new BorderLayout());
	add(basePanel);

	addComponentListener(new ComponentAdapter() {
	    @Override
	    public void componentResized(ComponentEvent arg0) {
		Utility.setLocationToMiddleOfScreen(Iam);
	    }
	});
    }


    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
    {
	UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	(new AccessRightsAssignmentWindow()).setVisible(true);
    }

    public JLabel getInformationLabel() {
	return informationLabel;
    }

    public JList getAvailableItemsView() {
	return availableItemsView;
    }

    public JList getAssignedItemsView() {
	return assignedItemsView;
    }

    public JButton getOkButton() {
	return okButton;
    }

    public JButton getAddButton() {
	return addButton;
    }

    public JButton getRemoveButton() {
	return removeButton;
    }

    public JCheckBox getReadCheckBox() {
	return readCheckBox;
    }

    public JCheckBox getWriteCheckBox() {
	return writeCheckBox;
    }

    private void removeFocus(Component component)
    {
	if(null != component)
	{
	    component.setFocusable(false);
	}
    }

}
